#pragma once
#include <QObject>
#include <QPointer>
#include <map>
#include <mutex>
#include <vector>

class QJsonObject;
class TdsCmdHandler;
class TdsReqHandler
{
public:
	TdsReqHandler();
    virtual ~TdsReqHandler();
    bool /*is_my_duty*/ webHandlerFunc(int browserId, qint64 frameId, const QString& category, const QJsonValue& value, int& error, QString& result);

protected:
    virtual void onSendDataToWeb(int browserId, qint64 frameId, const QString& res, const QString& cb);
private:
	std::map<QString, TdsCmdHandler*>	m_cmdHandlers;
	std::mutex	m_lock;
};

enum CmdType
{
	kLocalReq,
	KServerReq,
	kPush
};

struct ReqInfo
{
	QString m_jsCb;
	QString	m_reqContent;
    qint64  m_frameId;
    int     m_browserId = 0;
    int  	m_sid;

    void* m_webHandler = nullptr;

	ReqInfo(){}
    ReqInfo(QString cb, QString req, qint64 frame_id, int browserId)
	{
		m_jsCb = cb;
		m_reqContent = req;
        m_frameId = frame_id;
        m_browserId = browserId;
        m_sid = browserId;
	}
};

class TdsCmdHandler : public QObject
{
	Q_OBJECT
public:
    enum class TDS_OPERATE_TYPE: uint8_t {
        tds_reg = 0,
        tds_unreg = 1,
        tds_req = 2
    };
    Q_ENUM(TDS_OPERATE_TYPE)

    TdsCmdHandler(const CmdType& cmd_type):m_type(cmd_type) {}
    virtual ~TdsCmdHandler() {}
	CmdType type() const { return m_type; }

    virtual bool processCmd(const QJsonObject& cmdObj, const ReqInfo& reqinfo, bool unReg = false) = 0;

	static TdsCmdHandler* cmdHandler(const QString& cmd);
signals:
    void sendDataToWeb(qint64 frameId, int browserId, const QString& res, const QString& cb);

private:
    const CmdType m_type;
};


struct price_t;
extern QString displayPrice(const price_t* varPrice);
extern const char* getTfKey(const char* pId);
